package com.skyeyes.controller.user;

import com.skyeyes.constant.JwtClaimsConstant;
import com.skyeyes.entity.UserBean;
import com.skyeyes.properties.JwtProperties;
import com.skyeyes.result.Result;
import com.skyeyes.utils.JwtUtil;
import com.skyeyes.vo.LoginBeanVO;
import com.skyeyes.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "C端用户相关接口")
@Slf4j
public class UserController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private JwtProperties jwtProperties;


    /**
     * 登录
     * @param phone 手机号
     * @param password 密码
     * @return
     */
    @PostMapping("/mobilelogin")
    @ResponseBody
    @ApiOperation("手机登录")
    public Result login(String phone,String password) {
        UserBean user = loginService.login(phone, password);
        Map<String, Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        LoginBeanVO loginBeanVO = LoginBeanVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .password(user.getPassword())
                .phone(user.getPhone())
                .weilan("石家庄市")
                .token(token)
                .build();


        return Result.success(loginBeanVO);
    }

}
